package com.sunkyc.module.book.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunkyc.commons.entity.TablePageData;
import com.sunkyc.framework.system.entity.SysUser;
import com.sunkyc.framework.utils.TablePageDataBuilderUtil;
import com.sunkyc.commons.entity.PageQueryVo;
import com.sunkyc.framework.system.service.SysUserService;
import com.sunkyc.module.book.entity.SunkycBook;
import com.sunkyc.module.book.mapper.SunkycBookMapper;
import com.sunkyc.module.book.service.SunkycBookService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * <p>
 * 权限图书测试数据 服务实现类
 * </p>
 *
 * @author sunkyc
 * @since 2022-06-10
 */
@Service
@RequiredArgsConstructor
public class SunkycBookServiceImpl extends ServiceImpl<SunkycBookMapper, SunkycBook> implements SunkycBookService {
    private final SunkycBookMapper baseMapper;
    private final SysUserService sysUserService;



    @Override
    public TablePageData<SunkycBook> queryList(PageQueryVo pageQueryVo, SunkycBook sunkycBook) {
        Page<SunkycBook> page = new Page<>(pageQueryVo.getCurrent(),pageQueryVo.getPageSize());
        IPage<SunkycBook> iPage = baseMapper.selectPage(page,buildWrapper(sunkycBook));
        // 此处设置权限所有者，为了在模拟环境好区分。(有数据库损耗)
        if(CollectionUtil.isNotEmpty(iPage.getRecords())){
            iPage.getRecords().stream()
                    .forEach(item-> {
                        SysUser user = sysUserService.getById(item.getCreateBy());
                        item.setCreateByStr(ObjectUtil.isNotEmpty(user)?user.getNickName():"未知");
                    });
        }
        return TablePageDataBuilderUtil.buildTablePageData(iPage);
    }

    @Override
    public int addBook(SunkycBook sunkycBook) {
        return baseMapper.insert(sunkycBook);
    }

    @Override
    public int updateBook(SunkycBook sunkycBook) {
        return baseMapper.updateById(sunkycBook);
    }

    @Override
    public int delBook(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    private Wrapper<SunkycBook> buildWrapper(SunkycBook sunkycBook){
        return Wrappers.lambdaQuery(SunkycBook.class)
                .like(StrUtil.isNotBlank(sunkycBook.getBookName()),SunkycBook::getBookName,sunkycBook.getBookName())
                .eq(StrUtil.isNotBlank(sunkycBook.getBookIsbn()),SunkycBook::getBookIsbn,sunkycBook.getBookIsbn())
                .like(StrUtil.isNotBlank(sunkycBook.getBookAuthro()),SunkycBook::getBookAuthro,sunkycBook.getBookAuthro())
                .like(StrUtil.isNotBlank(sunkycBook.getBookCbs()),SunkycBook::getBookCbs,sunkycBook.getBookCbs());

    }
}
